package com.yilin.tms.order.controller;

import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.order.dispatch.DispatchOrder;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.party.OrderDispatchAutoConfig;
import com.yilin.tms.core.entity.order.party.OrderDispatchTask;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.organize.MemberType;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.order.service.order.IDispatchService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/order/dispatch/")
public class DispatchController extends BaseController {

    @Resource
    IDispatchService dispatchService;
    @Resource
    IUserRemote userRemote;

    /**
     * 获取派单分页 dispatchStatus
     * 排序时sort!=null，筛选时query!=null
     */
    @PostMapping(value = "getDispatchOrderPage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<DispatchOrder>> getDispatchOrderPage(DispatchOrder query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<DispatchOrder> pageData = dispatchService.getDispatchOrderPage(query,getLoginUser(),page,limit);
        return jsonView(true, "获取列表成功", pageData);
    }

    /**
     * 保存派单
     */
    @PostMapping(value = "saveDispatchOrder")
    @NotNull({ "priceMode", "settlementType", "dispatchOrderNoteList", "fullLoad",
            "autoDelivery", "autoAudit", "matching", "includedTax", "skipNeedReceipt", "startFence", "noBargain",
            "handLiquidation", "needUploadShipping", "needUploadShipping", "skipConfirmReceipt", "needUploadReceipt",
            "transportTypeName", "transportTypeCode"})
    @UserTarget(UserType.shipperUser)
    public ReturnData<Object> saveDispatchOrder(DispatchOrder dispatchOrder) {
//        if (StringUtil.isNotEmpty(dispatchOrder.getTruckTypeIds())) {
//            return jsonView(false, "需求车型名称不能为空");
//        }
        if (dispatchOrder.getPriceMode() == GoodsOrder.PriceMode.wholePrice) {
            if (dispatchOrder.getSettlementType() != GoodsOrder.SettlementType.releasePricing) return jsonErrView("整单必须为一口价结算");
            if (!dispatchOrder.getNoBargain()) return jsonErrView( "整单计价货源不能议价");
        } else if (dispatchOrder.getPriceMode() == GoodsOrder.PriceMode.unitPrice) {
            if (dispatchOrder.getSettlementType() == GoodsOrder.SettlementType.gatherTotal) return jsonErrView("单价计价货源不能汇总结算");
        } else if (dispatchOrder.getPriceMode() == GoodsOrder.PriceMode.notPrice){
            if ( dispatchOrder.getSettlementType() != GoodsOrder.SettlementType.gatherTotal)  return jsonErrView("汇总计价货源只能选择汇总结算");
        }
        if (!dispatchOrder.getNoBargain()) {
            MemberType shipperMemberType = ParamUtil.isSuccess(userRemote.getMyMemberTypeByOrganizeId(getLoginUser().getOrganizeId()));
            if (shipperMemberType.getReleasePaymentRate() != 0D) return jsonErrView("您需要发布付款，不支持议价模式");
        }
        dispatchService.saveDispatchOrder(dispatchOrder,getLoginUser());
        return jsonView(true, "保存成功");
    }

    /**
     * 一键派单
     */
    @PostMapping(value = "sureDispatchOrder")
    public ReturnData<Object> sureDispatchOrder(String[] ids) {
        OrderDispatchTask dispatchTask = dispatchService.initiateDispatchOrder(ids,getLoginUser());
        return jsonView(true, "派单成功",dispatchTask);
    }

    /**
     * 获取后台派单
     */
    @PostMapping(value = "getToDayOrderDispatchTask")
    public ReturnData<Object> getToDayOrderDispatchTask() {
        List<OrderDispatchTask> goingOrderDispatchTask = dispatchService.getToDayOrderDispatchTask(getLoginUser().getOrganizeId());
        return jsonView(true, "获取成功",goingOrderDispatchTask);
    }

    /**
     * 获取自动化配置
     */
    @PostMapping(value = "getDispatchAutoConfig")
    public ReturnData<OrderDispatchAutoConfig> getDispatchAutoConfig() {
        OrderDispatchAutoConfig config = dispatchService.getDispatchAutoConfig(getLoginUser());
        return jsonView(true, "一键派单成功",config);
    }

    /**
     * 更新自动化配置
     */
    @PostMapping(value = "updateDispatchAutoConfig")
    public ReturnData<Object> updateDispatchAutoConfig(OrderDispatchAutoConfig dispatchAutoConfig) {
        dispatchService.updateDispatchAutoConfig(dispatchAutoConfig,getLoginUser());
        return jsonView(true, "更新自动化配置成功");
    }

    /**
     * 删除派单
     */
    @PostMapping(value = "deleteDispatchOrder")
    @NotNull("id")
    @UserTarget(UserType.shipperUser)
    public ReturnData<Object> deleteDispatchOrder(DispatchOrder dispatchOrder) {
        DispatchOrder dispatchOrder1 = dispatchService.getById(dispatchOrder.getId());
        dispatchOrder1.setDispatchStatus(DispatchOrder.DispatchStatus.recycle);
        dispatchService.updateById(dispatchOrder1);
        return jsonView(true, "操作成功");
    }

    /**
     * 查询派单
     */
    @PostMapping(value = "selectDispatchOrder")
    @NotNull("id")
    @UserTarget(UserType.shipperUser)
    public ReturnData<Object> selectDispatchOrder(String id) {
        DispatchOrder dispatchOrder = dispatchService.selectDispatchOrder(id);
        return jsonView(true, "操作成功",dispatchOrder);
    }
}
